/*
 * @Autor: dc
 * @Date: 2022-03-16 13:15:53
 * @LastEditors: dc
 * @LastEditTime: 2022-03-21 14:29:31
 * @Description: 测试服务器
 */

const express = require('express')
const app = express();

const fs = require('fs')
const filePath = './api1.js'
// console.log(`正在监听 ${filePath}`);
fs.watchFile(filePath, (cur, prv) => {
    if (filePath) {
        // 打印出修改时间
        // 根据修改时间判断做下区分，以分辨是否更改
        if (cur.mtime != prv.mtime){
            // console.log(`cur.mtime>>${cur.mtime.toLocaleString()}`)
            // console.log(`prv.mtime>>${prv.mtime.toLocaleString()}`)
            console.log(`${filePath}文件发生更新`)
           
        }
    }
})


app.use((request, response, next) => {
    console.log('有人请求students服务器了');
    next()
})

app.get('/students', (request, response) => {
    const {keyWord} = request.query;
    const students = [
        { id: '001', name: '张黑娃' },
        { id: '002', name: '张白娃' },
        { id: '003', name: '张黄娃' },
        { id: '004', name: '小bb' },
        { id: '005', name: '老bb' },
    ]
    let arr = students.filter(item=>{
        return item.name.indexOf(keyWord)>=0 
    })
    response.send(arr)
})

app.listen(5000, (err) => {
    if (!err) {
        console.log('students服务器启动成功,服务器地址:http://localhost:5000/students')
    }
})